﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
namespace ConferenceRegister.Infrastructure
{
    public class MyMembershipProvider : MembershipProvider
    {
        private static string _ApplicationName { get { return "Kingdom Foundations"; } }

        public MembershipUser GetUser(string username)
        {
            string user = (WebConfigurationManager.AppSettings["Users"] ?? "").Split(new char[]{',',' '},StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(e=>e.ToLower() == username.ToLower());

            if (user == null)
                return
                new MembershipUser(
                    providerName: "MyMembershipProvider",
                    name: "",
                    providerUserKey: null,
                    email: "",
                    passwordQuestion: "",
                    comment: "",
                    isApproved: false,
                    isLockedOut: true,
                    creationDate: DateTime.UtcNow,
                    lastLoginDate: DateTime.UtcNow,
                    lastActivityDate: DateTime.UtcNow,
                    lastPasswordChangedDate: DateTime.UtcNow,
                    lastLockoutDate: DateTime.UtcNow);

            return
                new MembershipUser(
                    providerName: "MyMembershipProvider",
                    name: user,
                    providerUserKey: null,
                    email: "",
                    passwordQuestion: "",
                    comment: "",
                    isApproved: true,
                    isLockedOut: false,
                    creationDate: DateTime.UtcNow,
                    lastLoginDate: DateTime.UtcNow,
                    lastActivityDate: DateTime.UtcNow,
                    lastPasswordChangedDate: DateTime.UtcNow,
                    lastLockoutDate: DateTime.UtcNow);
        }

        public MembershipUser GetUserByEmail(string email)
        {
            return GetUser(email);
        }

        public override string ApplicationName
        {
            get
            {
                return _ApplicationName;
            }
            set { }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return false;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return false;
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 1;
            return new MembershipUserCollection() { GetUserByEmail(emailToMatch) };
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 1;
            return new MembershipUserCollection() { GetUserByEmail(usernameToMatch) };
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {

            List<string> users = (WebConfigurationManager.AppSettings["Users"] ?? "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            totalRecords = users.Count;
            MembershipUserCollection c = new MembershipUserCollection();
            users.ForEach(e => c.Add(GetUser(e)));
            return c;
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            return WebConfigurationManager.AppSettings["UserPassword"];
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return GetUser(username);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return GetUser(providerUserKey.ToString());
        }

        public override string GetUserNameByEmail(string email)
        {
            return GetUserByEmail(email).UserName;
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return int.MaxValue; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 1; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 8; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 10; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Clear; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return @"(?=.{6,})(?=(.*\d){1,})(?=(.*\W){1,})"; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return true; }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            return false;
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            return GetPassword(username,null) == password;
        }
    }
}